package com.gisuni.sddc.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.service.AdvQueryService;
import com.gisuni.sddc.service.RegionStaticService;
import com.gisuni.sddc.model.Response;

@IocBean(singleton = false)
@At("/client/service/RegionStaticController")
/**
 * 缓冲区 -行政区域
 *
 */
public class RegionStaticController {
	private Log log = Logs.getLog(RegionStaticController.class);
	@Inject
	private RegionStaticService  regionStaticService;
	
	@At("/landInfoStatics")
	@GET
	@POST
	@Ok("json")
	public Object landInfoStatics(@Param("params") String params,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		JSONObject jsonObj= new JSONObject(params);
		JSONArray tableArr = jsonObj.getJSONArray("table");
		Long totalNum=0L;
		Double totalArea=0D;
		try {
			for(int i=0;i<tableArr.length();i++){
				Map<String,Object> map=new HashMap<String,Object>();
				JSONObject tableObj= tableArr.getJSONObject(i);
				map = regionStaticService.staticLandNumAndArea(tableObj);
				map.put("landTypeName", tableObj.getString("landTypeName"));
				totalNum += (Long)map.get("NUM");
				totalArea += (Float)map.get("AREA");
				list.add(map);
			}
			BigDecimal bigDecimal =new BigDecimal(totalArea);
			totalArea = bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();  
			
			Map<String,Object> map=new LinkedHashMap<String,Object>();
			map.put("totalNum", totalNum);
			map.put("totalArea", totalArea);
			map.put("landInfoStatics", list);
			res.setResult(map);
			res.setSuccess(true);
		}catch (Exception e) {
			log.error(e.getMessage());
			Map<String,Object> map1=new HashMap<String,Object>();
			map1.put("message", e.getMessage());
			res.setResult(map1);
		}
		return res;
	}
	
	@At("/followUpProjectStatic")
	@GET
	@POST
	@Ok("json")
	public Object followUpProjectStatic(@Param("params") String params,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		JSONObject jsonObj= new JSONObject(params);
		JSONArray extendStyles=jsonObj.getJSONArray("extendStyleNames");
		Long totalNum=0L;
		Double totalArea=0D;
		try {
			for(int i=0;i<extendStyles.length();i++){
				Map<String,Object> map=new HashMap<String,Object>();
				jsonObj.put("currentQueryExtendStyleName", extendStyles.get(i));
				map = regionStaticService.staticExtendBlockNumAndArea(jsonObj);
				map.put("extendStyleName", jsonObj.getString("currentQueryExtendStyleName"));
				totalNum += (Long)map.get("NUM");
				totalArea += (Float)map.get("AREA");
				list.add(map);
			}
			BigDecimal bigDecimal =new BigDecimal(totalArea);  
			totalArea = bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();  
			Map<String,Object> map=new LinkedHashMap<String,Object>();
			map.put("totalNum", totalNum);
			map.put("totalArea", totalArea);
			map.put("ExtendStylesStatics", list);
			res.setResult(map);
			res.setSuccess(true);
		}catch (Exception e) {
			log.error(e.getMessage());
			Map<String,Object> map1=new HashMap<String,Object>();
			map1.put("message", e.getMessage());
			res.setResult(map1);
		}
		return res;
	}
	
	@At("/personCompletePercentStatic")
	@GET
	@POST
	@Ok("json")
	public Object personCompletePercentStatic(@Param("params") String params,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Map<String,Object> map=new HashMap<String,Object>();
		JSONObject jsonObj= new JSONObject(params);
		JSONArray companyNames=jsonObj.getJSONArray("companyNameArr");
		String startDate = jsonObj.getString("startDate");
		String endDate = jsonObj.getString("endDate");
		
		ArrayList<String> companyNameList=new ArrayList<String>();
		for(int i=0;i<companyNames.length();i++){
			companyNameList.add(companyNames.getString(i));
		}
		try {
			map=regionStaticService.personCompletePercentStatic(companyNameList,startDate,endDate);
			res.setResult(map);
			res.setSuccess(true);
		}catch(Exception e){
			log.error(e.getMessage());
			Map<String,Object> map1=new HashMap<String,Object>();
			map1.put("message", e.getMessage());
			res.setResult(map1);
		}
		return res;
	}
}
